package com.ry.novel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.bean.UserHolder;
import com.ry.novel.model.dto.AuthorInfo;
import com.ry.novel.model.dto.BookIndexInfo;
import com.ry.novel.model.dto.BookInfo;
import com.ry.novel.model.dto.UserInfo;
import com.ry.novel.model.dto.condition.CommonQuery;
import com.ry.novel.model.resp.Result;
import com.ry.novel.service.AuthorService;
import com.ry.novel.service.BookIndexService;
import com.ry.novel.service.BookService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ry
 * @since 2022-01-25 10:15
 **/
@Api(tags = "作者接口")
@RequestMapping("/author")
@RestController
public class AuthorController {
    @Autowired
    private AuthorService authorService;

    @Autowired
    private BookService bookService;

    @Autowired
    private BookIndexService bookIndexService;

    @GetMapping("/test")
    public String test(){
        System.out.println(UserHolder.userInfo.get());
        return "test";
    }

    @PostMapping("/toBeAuthor")
    public Result<String> toBeAuthor(@RequestBody AuthorInfo authorInfo){
        authorService.toBeAuthor(authorInfo);

        return Result.ok("注册成功");
    }

    @PostMapping("/authorHome")
    public Result<Page<BookInfo>> authorHome(@RequestBody CommonQuery commonQuery){
        return Result.ok(authorService.authorHome(commonQuery));
    }

    @PostMapping("/addBook")
    public Result<Void> addBook(@RequestBody BookInfo bookInfo){
        System.out.println(bookInfo);
        bookService.addBook(bookInfo);

        return Result.ok();
    }

    @PostMapping("/addBookIndex")
    public Result<Void> addBookIndex(@RequestBody BookIndexInfo bookIndexInfo){
        bookIndexService.authorPublishBookIndex(bookIndexInfo);

        return Result.ok();
    }

    @GetMapping("/bookIndexList")
    public Result<Map<String,List<BookIndexInfo>>> bookIndexList(@RequestParam("bookId") Long bookId){
        Map<String, List<BookIndexInfo>> map = bookIndexService.bookIndexList(bookId);


        return Result.ok(map);
    }

    /**
     * 作者接口
     * @param id
     * @return
     */
    @GetMapping("/indexContent")
    public Result<BookIndexInfo> indexContent(@RequestParam("id")Long id){
        return Result.ok(bookIndexService.queryById(id));
    }

    @PostMapping("/updateIndexContent")
    public Result<Void> updateIndexContent(@RequestBody BookIndexInfo bookIndexInfo){
        bookIndexService.authorUpdateBookIndex(bookIndexInfo);
        return Result.ok();
    }

    @PostMapping("/deleteIndex")
    public Result<Void> deleteIndex(@RequestParam("id")Long id){
        bookIndexService.deleteById(id);

        return Result.ok();
    }
}
